package com.ars.common.dao;

import com.ars.common.utils.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;


//提交活动材料
public class submitMaterialsMysql {
    public static void submit(String id, String userID, String url) throws SQLException {
        String sql = "INSERT INTO material(id, submitTime, userID, material) VALUES (?, ?, ?, ?)";

        // 获取当前时间并格式化
        LocalDateTime currentTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String formattedTime = currentTime.format(formatter);

        Connection connection = null;
        PreparedStatement ps = null;

        try {
            connection = DBUtil.getConnection();
            ps = connection.prepareStatement(sql);
            ps.setString(1, id);
            ps.setString(2, formattedTime);
            ps.setString(3, userID);
            ps.setString(4, url);

            ps.executeUpdate();
        } finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException e) {
                    // 处理关闭 PreparedStatement 时的异常
                    e.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (SQLException e) {
                    // 处理关闭 Connection 时的异常
                    e.printStackTrace();
                }
            }
        }
    }
}
